package com.yue.gmall.product.handler;

import com.yue.common.utils.R;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.HttpStatusCodeException;

import javax.validation.ValidationException;

/**
 * @author yuelinbo
 * @date 2020/4/25 23:35
 **/
@ControllerAdvice
public class ValidParamExceptionHandler {
   /* @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public R handleMethodArgumentNotValidException(MethodArgumentNotValidException exception){
        BindingResult bindingResult = exception.getBindingResult();
        StringBuilder error = new StringBuilder();
        if (bindingResult.getErrorCount() > 0) {
            for (ObjectError oe : bindingResult.getAllErrors()) {
                error.append(oe.getDefaultMessage()).append(";");
            }
        }
        return R.error(300, StringUtils.isEmpty(error) ? "数据不符合校验" : error.toString());
    }*/
   @ExceptionHandler(ValidationException.class)
   @ResponseBody
   public R handleMethodArgumentNotValidException(ValidationException  exception){
       String message = exception.getMessage();
       return R.error(300, StringUtils.isEmpty(message) ? "数据不符合校验" : message);
   }
}
